package com.elitedatai.mchtest.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;

public class CustomBeanUtil {

    public static <T> T copyProperties(T source, T target) {
        CopyOptions copyOptions = CopyOptions.create()
                .setIgnoreNullValue(true)
                .setIgnoreCase(false)
                .setFieldValueEditor((fieldName, fieldValue) -> {
                    // 如果是字符串类型且为空字符串，则返回null，这样就不会被复制
                    if (fieldValue instanceof String && StrUtil.isEmpty((String) fieldValue)) {
                        return null;
                    }
                    return fieldValue;
                });
        BeanUtil.copyProperties(source, target, copyOptions);
        return target;
    }
}
